package com.punai.web.controller.system;

import com.punai.common.annotation.Log;
import com.punai.common.constant.UserConstants;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.domain.entity.EcoCompany;
import com.punai.common.enums.BusinessType;
import com.punai.common.utils.SecurityUtils;
import com.punai.system.service.IEcoCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/system/ecoCompany")
public class EcoCompanyController extends BaseController {

    @Autowired
    private IEcoCompanyService ecoCompanyService;


    /**
     * 获取公司列表
     */
    @PreAuthorize("@ss.hasPermi('system:ecoCompany:list')")
    @GetMapping("/list")
    public AjaxResult list(EcoCompany company)
    {
        List<EcoCompany> companies = ecoCompanyService.selectCompanyList(company);
        return AjaxResult.success(companies);
    }

    /**
     * 获取公司列表
     */
    @GetMapping("/treelist")
    public AjaxResult treelist()
    {
        EcoCompany companies = ecoCompanyService.treeCompanyList();
        List<EcoCompany> arrayList = new ArrayList<>();
        arrayList.add(companies);
        return AjaxResult.success(arrayList);
    }

    /**
     * 查询公司列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('system:ecoCompany:list')")
    @GetMapping("/list/exclude/{companyId}")
    public AjaxResult excludeChild(@PathVariable(value = "companyId", required = false) Long id)
    {
        List<EcoCompany> depts = ecoCompanyService.selectCompanyList(new EcoCompany());
//        depts.removeIf(d -> d.getId().equals(id) || ArrayUtils.contains(StringUtils.split(d.getPids(), ","), id + ""));
        return AjaxResult.success(depts);
    }

    /**
     * 根据公司编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ecoCompany:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable String id)
    {
        ecoCompanyService.checkCompanyDataScope(id);
        return AjaxResult.success(ecoCompanyService.selectCompanyById(id));
    }

    /**
     * 根据公司编号获取部门信息
     */
    @PreAuthorize("@ss.hasPermi('system:ecoCompany:query')")
    @GetMapping(value = "/getDeptInfo/{id}")
    public AjaxResult getDeptInfo(@PathVariable String id)
    {
        return AjaxResult.success(ecoCompanyService.selectDeptById(id));
    }

    /**
     * 新增公司
     */
    @PreAuthorize("@ss.hasPermi('system:ecoCompany:add')")
    @Log(title = "公司管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody EcoCompany company)
    {
//        if (UserConstants.NOT_UNIQUE.equals(ecoCompanyService.checkCompanyNameUnique(company)))
//        {
//            return AjaxResult.error("新增公司'" + company.getCompanyName() + "'失败，公司名称已存在");
//        }
//        if (UserConstants.NOT_UNIQUE.equals(ecoCompanyService.checkCompanyPrxUnique(company)))
//        {
//            return AjaxResult.error("新增公司'" + company.getPrx() + "'失败，公司简称已存在");
//        }
        return toAjax(ecoCompanyService.insertCompany(company));
    }

    /**
     * 修改公司
     */
    @PreAuthorize("@ss.hasPermi('system:ecoCompany:edit')")
    @Log(title = "公司管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody EcoCompany company)
    {
        String id = company.getId();
        ecoCompanyService.checkCompanyDataScope(id);
        if (UserConstants.NOT_UNIQUE.equals(ecoCompanyService.checkCompanyNameUnique(company)))
        {
            return AjaxResult.error("修改公司'" + company.getCompanyName() + "'失败，公司名称已存在");
        }
        return toAjax(ecoCompanyService.updateCompany(company));
    }

    /**
     * 删除公司
     */
    @PreAuthorize("@ss.hasPermi('system:ecoCompany:remove')")
    @Log(title = "公司管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        return toAjax(ecoCompanyService.deleteCompanyById(id));
    }


    /**
     * 根据当前公司前缀
     */
    @GetMapping(value = "/getCurrentPrx")
    public AjaxResult getCurrentPrx()
    {
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        if (admin) {
            return AjaxResult.success("manager");
        }
        Long companyId = SecurityUtils.getCompanyId();
        EcoCompany sysCompany = ecoCompanyService.selectCompanyById(companyId.toString());
        return AjaxResult.success("操作成功",sysCompany.getPrx());
    }

    /**
     * 根据当前公司编号
     */
    @GetMapping(value = "/getCurrentId")
    public AjaxResult getCurrentId()
    {
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        if (admin) {
            return AjaxResult.success(1L);
        }
        Long companyId = SecurityUtils.getCompanyId();
        return AjaxResult.success("操作成功",companyId);
    }

    /**
     * 类型
     */
    @GetMapping(value = "/listType")
    public AjaxResult getListType()
    {
        return AjaxResult.success(ecoCompanyService.getListType());
    }

    /**
     * 类型细分 路桥 安装
     */
    @GetMapping(value = "/sourceType")
    public AjaxResult getSourceType()
    {
        return AjaxResult.success(ecoCompanyService.getZyType());
    }



}
